package com.aaa.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * fileName:DateUtil
 * description:
 *
 * @author:wzl createTime:2020/9/4 22:16
 */
public class DateUtil {
        /**
        * 时间转字符串,默认
        * @param date
        * @return
         */
    public static String dateToString(Date date){
        if(date!=null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return simpleDateFormat.format(date);
        }
        return null;
    }

    /**
     *自定义格式转换
     * @param date
     * @param format  "yyyy/MM/dd HH:mm:ss"
     * @return
     */
    public static String dateToString(Date date,String format){
        if(date!=null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        }
        return null;
    }

    /**
     * 时间格式字符串转时间
     * @param dateStr
     * @return
     */
    public static Date  stringToDate(String dateStr){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
